import com.tc.jgit.GitUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.ReflogReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.Test;

public class testJgit {

    @Test
    public void demo1() throws Exception{ //获取全部提交
        String uri = "http://cai.tian@10.199.133.181:9999/vpal-new/beifu_payment/push-aml";
        String username = "cai.tian";
        String password = "m2KL23955Nh";
        String localDir = "D:\\Git\\push-aml";
        Git git = GitUtil.getGit(uri,localDir, GitUtil.getCredentialsProvider(username, password));
        Repository repository = git.getRepository();
        RevWalk revWalk = null;
        try {
            revWalk = new RevWalk(repository);
            ObjectId commitId = repository.resolve("origin/release");
            revWalk.markStart(revWalk.parseCommit(commitId));
            for(RevCommit commit : revWalk) {
                System.out.println(commit.getFullMessage());
                System.out.println(commit.getId().getName());
            }
        }finally {
           if(revWalk!=null) revWalk.close();
        }
    }

    @Test
    public void demo2() throws Exception{ //获取最后一次提交
        String uri = "http://cai.tian@10.199.133.181:9999/vpal-new/beifu_payment/push-aml";
        String username = "cai.tian";
        String password = "m2KL23955Nh";
        String localDir = "D:\\Git\\push-aml";
        Git git = GitUtil.getGit(uri,localDir, GitUtil.getCredentialsProvider(username, password));
        ReflogReader release = git.getRepository().getReflogReader("origin/release");
        ReflogEntry lastEntry = release.getLastEntry();
        String comment = lastEntry.getComment();
        System.out.println(lastEntry.getNewId().getName());
        System.out.println(comment);
    }


}
